<?php
class Admin_UserController extends Zendhlt_Controller_AdminAction {

	const ACTIVE = 0;

	private $_currentUser;
	private $_arrParam;
	private $_paginator = array();
	private $_actionMain;
	private $_namespace;

	public function init() {
		$layoutPath = TEMPLATE_PATH . '/admin';
		$option = array('layout'     => 'index',
                   		'layoutPath' => $layoutPath);
		Zend_Layout::startMvc($option);

		// Phân trang
		$this->_paginator['itemCountPerPage'] = 5;
		$this->_paginator['pageRange'] 		  = 3;
		$this->_paginator['currentPage'] 	  = $this->_request->getParam('page', 1);

		// Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();

		// Duong dan cua Action chinh
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];

		// Dat ten SESSION
		// Luu cac du lieu filter vao SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);

		if(empty($ssFilter->col) || ($this->_request->getParam('resetfilter', 0) == 1)) {
			$ssFilter->keywords 	= '';
			$ssFilter->col 			= 'ngay_tao';
			$ssFilter->order 		= 'DESC';
			$ssFilter->state 		= self::ACTIVE;
			$ssFilter->error 		= 0;
		}

		$this->_arrParam['paginator']			 = $this->_paginator;
		$this->_arrParam['ssFilter']['keywords'] = $ssFilter->keywords;
		$this->_arrParam['ssFilter']['col'] 	 = $ssFilter->col;
		$this->_arrParam['ssFilter']['order'] 	 = $ssFilter->order;
		$this->_arrParam['ssFilter']['state'] 	 = $ssFilter->state;

		// Truyen ra view
		$this->view->actionMain = $this->_actionMain;
		$this->view->currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];
		$this->view->arrParam = $this->_arrParam;

		// Lay thong tin user admin hien tai
		$auth = Zend_Auth::getInstance();
		$this->_currentUser = $auth->getIdentity();
	}

	public function indexAction() {
		$this->view->headTitle('Danh sách user');
		
		$model = new Admin_Model_UsersMapper();
		$paginator = new Zendhlt_Paginator();
		
		$users = $model->fetch($this->_arrParam);
		$totalItem = $model->countItem($this->_arrParam);
		
		$this->view->panigator = $paginator->createPaginator($totalItem, $this->_paginator);
		$this->view->totalItems = $totalItem;
		$this->view->Items = $users;

		// Display error if delete occur error {
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		$this->view->msg = '';
		
		if ($ssFilter->error == 1) {
			$this->view->msg = 'Xuất hiện lổi trong quá trình thực hiện!';
			$ssFilter->error = 0;
		}
	}
	
	public function filterAction() {
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
	
		if ($this->_arrParam['type'] == 'search') {
			if($this->_arrParam['key'] == 1) {
				$ssFilter->keywords = trim($this->_arrParam['keywords']);
				$ssFilter->state = $this->_arrParam['state'];
			} else {
				$ssFilter->keywords = '';
				$ssFilter->state = self::ACTIVE;
			}
		}
	
		if ($this->_arrParam['type'] == 'order') {
			$ssFilter->col = $this->_arrParam['col'];
			$ssFilter->order = $this->_arrParam['by'];
		}
	
		$this->_redirect($this->_actionMain . '/index');
		$this->_helper->viewRenderer->setNoRender();
	}
	
	// Update trang thai ẩn|hiện
	public function deleteAction() {
		$model = new Admin_Model_UsersMapper();
	
		if (isset($this->_arrParam['id'])) {
			$id = $this->_arrParam['id'];
		} elseif (isset($this->_arrParam['cid'])) {
			$id = $this->_arrParam['cid'];
		}
	
		if ($id != null) {
			$state = $this->_arrParam['delete-type'];
	
			if ($model->deleteItems($id, $state, $this->_currentUser->id) == false) {
				$ssFilter = new Zend_Session_Namespace($this->_namespace);
				$ssFilter->error = 1;
			}
		}
	
		$this->_redirect($this->_actionMain . '/index');
		$this->_helper->viewRenderer->setNoRender();
	}

}